package com.biz.solr.aspectj;

import com.biz.solr.annotation.DeleteBody;
import com.biz.solr.annotation.DeleteKey;
import com.biz.solr.annotation.SolrSearchDelete;
import com.biz.solr.biz.conifg.BIZConfig;
import com.biz.solr.exceptions.BizSolrDeleteException;
import com.biz.solr.exceptions.BizSolrNotFoundBizConifgException;
import com.biz.solr.utils.MapUtil;
import com.biz.solr.utils.SolrContextHelper;
import com.biz.solr.utils.BizSolrUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @ClasName SearchDeleteAspectj
 * @Descripution TODO 在方法正常执行后 执行Solr删除 根据业务关系的字段 构造条件
 * @Author USER
 * @Date 2022/3/24 8:41
 * @Version V1.0
 */
@Aspect
@Component
public class SearchDeleteAspectj {

    @Autowired
    BizSolrUtils solrUtils;

    /**
     * 切入点
     * 切入点为注解的： @annotation(SolrSearchDelete)
     * 存在 SolrSearchDelete 注解的方法
     */
    @Pointcut("@annotation(com.biz.solr.annotation.SolrSearchDelete)")
    private void deletePointCut() {
    }

    /**
     * 在方法正常执行通过之后执行的通知
     * @param joinPoint
     */
    @AfterReturning("deletePointCut()")
    public void doAfterReturning(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        SolrSearchDelete solrSearchDelete = method.getAnnotation(SolrSearchDelete.class);
        String bizType = solrSearchDelete.bizType(); //根据不同的核心去更新对应的

        HashMap<String, Object> wheres = new HashMap<>();

        int bodyCount = 0;
        int keyCount = 0;

        Annotation[][] parameterAnnotations = method.getParameterAnnotations();

        for (Annotation[] parameterAnnotation: parameterAnnotations) {
            int paramIndex= ArrayUtils.indexOf(parameterAnnotations, parameterAnnotation);
            for (Annotation annotation: parameterAnnotation) {
                if (annotation instanceof DeleteBody){
                    bodyCount++;
                    Object paramValue = args[paramIndex];
                    wheres.putAll(MapUtil.beanToMap(paramValue));
                }else if(annotation instanceof DeleteKey){
                    keyCount++;
                    Object paramValue = args[paramIndex];
                    String filedName = ((DeleteKey) annotation).FiledName();
                    wheres.put(filedName,paramValue);
                }
            }
        }
        if(bodyCount == 0 && keyCount == 0){
            if(SolrContextHelper.getSolrContextWhere().isEmpty()){
                throw new BizSolrDeleteException("参数不存在！");
            }else{
                //使用帮助类直接构造条件
                wheres.putAll(SolrContextHelper.getSolrContextWhere());
            }
        }else if( bodyCount >= 1 && keyCount>=1 ){
            throw new BizSolrDeleteException("DeleteBody与DeleteKey不能同时存在！");
        }else if( bodyCount > 1 ){
            throw new BizSolrDeleteException("DeleteBody只能存在一个！");
        }

        //更新自己与对应的业务
        BIZConfig searchBizByCore = BIZConfig.getSearchBizByBiz(bizType);
        if(searchBizByCore == null){
            throw new BizSolrNotFoundBizConifgException("找不到对应业务的配置");
        }

        solrUtils.solrDeleteByCoreAndHashMap(searchBizByCore,wheres);
    }



}

